var app = getApp()

import {
  list as signInList
} from '../../api/signIn'

import {
  unReadCount,
} from '../../api/msgPush'

import { list as wxPageList } from "../../api/wxPage";
import {
  getInfo,
} from '../../api/member'
Component({
  data: {
    vipType: 0,
    orderCount: "",
    unReadNum: "", //未读消息数
    showUnReadNum: "", //显示的未读数量
    purchaseId: '',
    purchase: app.globalData.purchase,
    groupBuyDisplay: app.globalData.groupBuyDisplay,
    SCOPE_TYPE: app.SCOPE_TYPE,
    qrcodeLogin: app.QRCODELOGIN,
    imagePath: 'http://cdnx.rzico.com/10200/functions/',
    //可配置：url:跳转页面,event:触发事件名,tabBar:配合 url 使用跳转 tabBar 页面
    functionList: [
      {
        name: '收货地址',
        image: 'address',
        url: '/pages/functions/jumpPage/address/address',
      }, {
        name: '我的空桶',
        image: 'myEmptyBarrel',
        url: '/pages/member/emptyBarrel/emptyBarrel',
        scopeType: 2,
      }, {
        name: '我的水票',
        image: 'coupon',
        url: '/pages/tabBar/water/water',
        scopeType: 2,
        tabBar: true
      }, {
        name: '叫水码',
        image: 'callWaterCode',
        url: '/pages/functions/jumpPage/callWaterCode/callWaterCode',
        scopeType: 2,
      }, {
        name: '会员卡',
        image: 'saveCard',
        url: '/pages/wallet/jumpPage/saveCard/index',
        scopeType: 1,
      }, {
        name: '邀请码',
        image: 'callWaterCode',
        event: 'canvasClick',
        scopeType: 1,
      }, {
        name: '优惠券',
        image: 'coupon',
        url: '/pages/member/coupon/coupon',
        scopeType: 1,
      }, {
        name: '红包收入',
        image: 'wallet',
        url: '/pages/wallet/jumpPage/distribution/index',
      }, {
        name: '加入我们',
        image: 'joinUs',
        url: '/pages/functions/jumpPage/joinUs/joinUs',
      }, {
        name: '我的客户',
        image: 'myMember',
        url: '/pages/functions/jumpPage/myInvitation/list',
      }, {
        name: '我的业绩',
        image: 'myAcmp',
        // url: '/pages/functions/jumpPage/popularize/list',
        url: '/pages/functions/jumpPage/popularize/performance/performance'
      },
    ]
  },

  lifetimes: {
    ready() {
      this.data.SCOPE_TYPE = app.SCOPE_TYPE
      this.setData({
        SCOPE_TYPE: this.data.SCOPE_TYPE
      })
    },
    attached: function () {
      this.loadWxpage()
      this.getInfo()
    },
  },

  pageLifetimes: {
    show: function () {
      // 页面被展示
      console.info("页面被展示了")
      this.getunReadCount()
      // this.hitCountClick()
      this.getInfo()
    },
    hide: function () {
      // 页面被隐藏
    },
    resize: function (size) {
      // 页面尺寸变化
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    toLink(e) {
      const { url, event, tabBar } = e.currentTarget.dataset.item
      this.checkLogin(() => {
        console.log(url, event, tabBar);
        if (event != '') {
          this.triggerEvent(`${event}`)
        }
        if (url != '') {
          if (tabBar == true) {
            wx.switchTab({
              url: `${url}`
            })
          } else {
            wx.navigateTo({
              url: `${url}`
            })
          }
        }
      })
    },
    toEmptyBarrel: function (e) { //我的空桶
      this.checkLogin(() => {
        if (app.VERSION === 0 || app.VERSION === 2) {
          this.checkLogin(() => {
            wx.navigateTo({
              url: '/pages/member/shopCard/shopCard',
              success: function (res) { },
              fail: function (res) { },
              complete: function (res) { },
            })
          })
        } else if (app.VERSION === 1 || app.VERSION === 4) {
          this.checkLogin(() => {
            wx.navigateTo({
              url: '/pages/member/emptyBarrel/emptyBarrel',
              success: function (res) { },
              fail: function (res) { },
              complete: function (res) { },
            })
          })
        }
      })
    },
    getInfo() {
      var _this = this
      getInfo().then(
        res => {
          if (res.type == 'success') {
            this.data.vipType = res.data.member.vipType
            this.setData({
              vipType: this.data.vipType,
            })
          }
        }
      ).catch(error => {
      })
    },

    groupClick: function () {
      wx.navigateTo({
        url: '/pages/functions/jumpPage/groupbuy/groupOrder',
      })
    },
    getunReadCount() {
      unReadCount().then(
        res => {
          console.log("接收用户信息--" + JSON.stringify(res))
          if (res.type == 'success') {
            let unReadNum = 0
            if (res.data.length > 0) {
              res.data.map(item => {
                unReadNum += item.msg_num
              })
            }
            this.setData({
              unReadNum: unReadNum,
              showUnReadNum: unReadNum > 100 ? '...' : unReadNum
            })
            console.log("unReadNum--" + this.data.unReadNum)
          }
        }
      ).catch(error => {

      })
    },
    toShareCenter() {
      if (app.QRCODELOGIN == false) {
        this.checkLogin(() => {
          wx.navigateTo({
            url: '/pages/functions/jumpPage/share/share',
          })
        })
      }
    },
    loadWxpage() {
      wxPageList({
        mchId: app.globalData.mchId,
        pageNum: 1,
        pageSize: 20,
        sortField: 'orders',
        direction: 'asc'
      }).then(res => {
        console.log(res)
        res.data.data.forEach(item => {
          if (item.orders == 9) {
            this.data.purchaseId = item.id
            this.setData({
              purchaseId: this.data.purchaseId
            })
          }
        })
      })
    },
    toPurchase(e) {
      this.checkLogin(() => {
        if (this.data.purchaseId != null && this.data.purchaseId != "") {
          wx.navigateTo({
            url: '/pages/index/wxPage/wxPage?id=' + this.data.purchaseId
          })
        }
      })
    },
    toBank(e) {
      this.checkLogin(() => {
        wx.navigateTo({
          url: '/pages/functions/jumpPage/collectionCode/list',
        })
      })
    },
    messageClick() {
      this.checkLogin(() => {
        wx.navigateTo({
          url: '/pages/member/message/index',
        })
      })

    },

    //收货地址
    toAddress: function (e) {
      this.checkLogin(() => {
        wx.navigateTo({
          url: '/pages/functions/jumpPage/address/address',
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      })
    },

    toSignIn: function (e) {
      this.checkLogin(() => {
        signInList().then(res => {
          if (res.data.data.length > 0) {
            wx.navigateTo({
              url: '/pages/functions/jumpPage/signIn/signIn?index=' + 1,
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: '暂无活动',
            })
          }
        }).catch(res => {
          wx.showToast({
            icon: 'none',
            title: '暂无活动',
          })

        })
      })
    },
    canvasClick: function (e) {
      this.checkLogin(() => {
        wx.navigateTo({
          url: '/pages/functions/jumpPage/callWaterCode/callWaterCode',
        })
      })
    },

    //检测用户的登录状态
    checkLogin(callback) {
      console.info("全局变量app.globalData--" + JSON.stringify(app.globalData))
      if (app.globalData.token == null) {
        wx.navigateTo({
          url: '/pages/login/index',
        })
      } else {
        if (callback) {
          callback()
        }
      }
    },
  }
})
